package com.wcl.demo.ui.activity;


import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.easemob.EMCallBack;
import com.easemob.chat.EMChatManager;
import com.easemob.chat.EMChatOptions;
import com.wcl.demo.R;
import com.wcl.demo.utils.ActivityUtils;
import com.wcl.demo.utils.AppHelper;
import com.wcl.demo.view.CustomToast;
import com.wcl.demo.view.SwitchButton;

import butterknife.OnClick;


/**
 * Created by dllo on 15/11/19.
 */
public class SetActivity extends BaseActivity implements CompoundButton.OnCheckedChangeListener, View.OnClickListener {

    private SwitchButton receiveSB, soundSB, shakeSB, reviewSoundSB, allowSB;//定义开关
    private EMChatOptions options;
    private SharedPreferences sp;//定义缓存文件
    private SharedPreferences.Editor editor;//定义缓存文件的editor
    private Button button;//定义Button
    private RelativeLayout sound_relative, shake_relative;
    private TextView line1, line3, personInforTv , checked_networkTv;



    @Override
    protected int getLayout() {
        return R.layout.activity_set;
    }

    @Override
    protected void initView() {
        receiveSB = (SwitchButton) findViewById(R.id.receiveSB);
        receiveSB.setOnCheckedChangeListener(this);//接收新消息开关
        soundSB = (SwitchButton) findViewById(R.id.soundSB);
        soundSB.setOnCheckedChangeListener(this);//接收新消息提醒开关
        shakeSB = (SwitchButton) findViewById(R.id.shakeSB);
        shakeSB.setOnCheckedChangeListener(this);//定义新消息震动
        reviewSoundSB = (SwitchButton) findViewById(R.id.reviewSoundSB);
        reviewSoundSB.setOnCheckedChangeListener(this);//定义使用听筒播放
        button = (Button) findViewById(R.id.Logout_login);
        button.setOnClickListener(this);//定义退出登录
        sound_relative = (RelativeLayout) findViewById(R.id.sound_relative);
        shake_relative = (RelativeLayout) findViewById(R.id.shake_relative);
        line1 = (TextView) findViewById(R.id.line1);
        line3 = (TextView) findViewById(R.id.line3);
        personInforTv = (TextView) findViewById(R.id.personInforTv);//定义个人资料
        personInforTv.setOnClickListener(this);
        checked_networkTv = (TextView) findViewById(R.id.checked_networkTv);
        checked_networkTv.setOnClickListener(this);//定义检查网络
    }

    @Override
    protected void initData() {
        options = EMChatManager.getInstance().getChatOptions();
        sp = this.getSharedPreferences("button_state", Context.MODE_WORLD_READABLE);
        editor = sp.edit();
        if (sp.getBoolean("RS_ISCHECKED", true)) {
            receiveSB.setChecked(sp.getBoolean("RS_ISCHECKED", false));
            Log.i("searchhim", receiveSB.isChecked() + "");
            soundSB.setChecked(sp.getBoolean("SS_ISCHECKED", false));
            Log.i("searchhim", soundSB.isChecked() + "");
            shakeSB.setChecked(sp.getBoolean("SHS_ISCHECKED", false));
            Log.i("searchhim", shakeSB.isChecked() + "");
        }
        if (sp.getBoolean("RSSB_ISCHECKED", true)) {
            reviewSoundSB.setChecked(sp.getBoolean("RSSB_ISCHECKED", false));
        }
    }

    @OnClick(R.id.activity_set_bac_Iv)
    void activity_set_bac_Iv() {
        finish();
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        switch (buttonView.getId()) {
            case R.id.receiveSB://接收新消息提醒
                if (isChecked) {
                    editor.putBoolean("RS_ISCHECKED", true).commit();
                    options.setNotifyBySoundAndVibrate(true);
                    sound_relative.setVisibility(View.VISIBLE);
                    shake_relative.setVisibility(View.VISIBLE);
                    line1.setVisibility(View.VISIBLE);
                    line3.setVisibility(View.VISIBLE);
                    Log.i("searchhim", "以选中接收新消息");
                } else {
                    editor.putBoolean("RS_ISCHECKED", false).commit();
                    options.setNotifyBySoundAndVibrate(false);
                    soundSB.setChecked(false);
                    shakeSB.setChecked(false);
                    sound_relative.setVisibility(View.GONE);
                    shake_relative.setVisibility(View.GONE);
                    line1.setVisibility(View.GONE);
                    line3.setVisibility(View.GONE);
                    Log.i("searchhim", "未选中接收新消息");
                }
                break;
            case R.id.soundSB://接收提醒声音
                if (isChecked) {
                    editor.putBoolean("SS_ISCHECKED", true).commit();
                    options.setNoticeBySound(true);
                    Log.i("searchhim", "以选中声音提醒");

                } else {
                    editor.putBoolean("SS_ISCHECKED", false).commit();
                    options.setNoticeBySound(false);
                    Log.i("searchhim", "未选中声音提醒");

                }
                break;
            case R.id.shakeSB://提醒震动
                if (isChecked) {
                    editor.putBoolean("SHS_ISCHECKED", true).commit();
                    options.setNoticedByVibrate(true);
                    Log.i("searchhim", "以选中震动提醒");

                } else {
                    editor.putBoolean("SHS_ISCHECKED", false).commit();
                    options.setNoticedByVibrate(false);
                    Log.i("searchhim", "未选中震动提醒");
                }
                break;
            case R.id.reviewSoundSB://扬声器
                if (isChecked) {
                    editor.putBoolean("RSSB_ISCHECKED", true).commit();
                    options.setUseSpeaker(true);
                } else {
                    editor.putBoolean("RSSB_ISCHECKED", false).commit();
                    options.setUseSpeaker(false);
                }
                break;
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.Logout_login://退出登录
                AppHelper.getInstance().logout(false, null);
                ActivityUtils.startToActivity(this, LoginActivity.class);
                finish();
                break;
            case R.id.personInforTv://个人资料
                Intent intent = new Intent(this, PersonInforActivity.class);
                String username = EMChatManager.getInstance().getCurrentUser();
                intent.putExtra("username", username);
                startActivity(intent);
                break;
            case R.id.checked_networkTv:
                CustomToast.toastShow(this , "网络正常");
                break;
        }
    }
}
